import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/login",
      component: () => import("../pages/login/Index.vue"),
    },
    {
      path: "/home",
      component: () => import("../pages/home/Index.vue"),
      children: [
        {
          path: "/welcome",
          component: () => import("../pages/home/welcome/Index.vue"),
        },
        {
          path: "/swper",
          component: () => import("../pages/home/swper/Index.vue"),
        },
        {
          path: "/users",
          component: () => import("../pages/home/users/Index.vue"),
        },
        {
          path: "/classify",
          component: () => import("../pages/home/productClassify/Index.vue"),
        },
        {
          path: "/detail",
          component: () => import("../pages/home/productDetail/Index.vue"),
        },
        {
          path: "/charts",
          component: () => import("../pages/home/charts/Index.vue"),
        },
      ],
      // 路由
      beforeEnter(to, from, next) {
        // 路由鉴权
        if (localStorage.getItem("token")) {
          next();
        } else {
          next("/login");
        }
      },
    },
    {
      path: "/:pathMatch(.*)*",
      component: () => import("../pages/notFound/Index.vue"),
    },
  ],
});

export default router;
